import { randomUUID } from "node:crypto";
import { range } from "ramda";
import { HealthDescEnum, HealthEnum } from "../model/v3pro.enum";
import fs from "node:fs";

export const v3proMockScreenAllStatus = (): any => {
  return {
    deviceInfo: {
      id: randomUUID(),
      name: "screen-1",
      type: 65280,
      typeName: "SCREEN_FULL_COLOR",
      model: "screen",
      secondBrightnessEnable: false,
    },
    layout: {
      x: 0,
      y: 0,
      width: 3840,
      height: 2160,
    },
    sendBoxs: range(0, 4).map((i) => mockSendBox(i, 2, 2)),
  };
};

const genHealthDescription = (i: number) => {
  const keys = Object.keys(HealthDescEnum);
  const values = Object.values(HealthDescEnum);
  return values[i % keys.length];
};

const mockSendBox = (i: number, col: number, row: number) => {
  const id = randomUUID();
  const [width, height] = [1920, 1080];
  const res = {
    deviceInfo: {
      id,
      name: "V3FS01",
      type: 90,
      typeName: "V3FS01",
      model: "sendBox",
      addr: "202.11.209." + i,
    },
    addr: "202.11.209." + i,
    status: {
      ioInputArr: [
        {
          ioIndex: 0,
          status: "unknown", // unknown | on | off
        },
      ],
      health: HealthEnum.NORMAL,
      healthDesc: {
        status: HealthEnum.NORMAL,
        description: [HealthDescEnum.NORMAL], //genHealthDescription(i),
      },
      communication: {
        control: {
          status: "OK",
        },
        interfacesSta: range(0, 5).map((i) => mockOutPortSta(i)),
        opticalFibersSta: range(1, 5).map((i) => mockOutPortSta(i)),
      },
      runStatus: {
        voltages: [
          {
            name: "voltage1",
            value: "12.00",
            index: 0,
          },
        ],
        workingTempC: 46,
        uptimeS: 151598,
        queryTime: "2024-06-22T01:52:04.998Z",
        queryTimeMS: 1719021124998,
      },
    },
    layout: {
      outputMode: "net port", // "optical fiber"
      name: "V3FS01",
      id,
      type: 90,
      ip: "202.11.209." + i,
      width,
      height,
      x: (i % col) * width,
      y: Math.floor(i / col) * height,
      outPorts: range(0, 4).map((i) => mockOutPorts(i, 2, 2)),
    },
    scanBoards: [] as any[],
  };
  res.scanBoards = res.layout.outPorts.map((port) => port.scanBoards).flat();
  return res;
};

const mockOutPortSta = (i: number) => {
  return {
    receivedUpdFrame: true,
    hasErrorCode: false,
    linked: true,
    bandWidth: "1G",
    linkChangeCount: 1,
    netChipConfSuccess: true,
    interfaceIndex: i,
    name: "Control Interface",
    queryTime: "2024-10-22T03:05:50.464Z",
    healthStatus: "OK",
  };
};
const mockOutPorts = (i: number, col: number, row: number) => {
  const [width, height] = [960, 540];
  return {
    name: "net1",
    x: (i % col) * width,
    y: Math.floor(i / col) * height,
    width,
    height,
    originationIP: "192.168.144." + i,
    scanBoards: range(0, 4).map((i) => mockScanBoards(i, 2, 2)),
  };
};

const mockScanBoards = (i: number, col: number, row: number) => {
  const id = randomUUID();
  const [width, height] = [480, 270];
  return {
    id,
    name: "ScanBoard_" + i,
    type: 88,
    typeName: "V3CG02",
    deviceInfo: {
      id,
      name: "ScanBoard_" + i,
      type: 88,
      typeName: "V3CG02",
      model: "scanBoard",
      addr: "192.168.144." + i,
    },
    layout: {
      name: "ScanBoard_" + i,
      id,
      ip1: "192.168.144." + i,
      ip2: "192.168.144." + i,
      x: (i % col) * width,
      y: Math.floor(i / col) * height,
      width,
      height,
    },
    addr: "192.168.144." + i,
    status: {
      panelConf: {
        width: 120,
        height: 135,
      },
      communication: {
        control: {
          status: "OK",
        },
      },
      addr: "192.168.144." + i,
      health: HealthEnum.NORMAL,
      healthDesc: {
        status: HealthEnum.NORMAL,
        description: [HealthDescEnum.NORMAL], //genHealthDescription(i),
      },
      runStatus: {
        voltages: [
          {
            name: "voltage1",
            value: "12.00",
            index: 0,
          },
        ],
        workingTempC: 46,
        uptimeS: 151598,
        queryTime: "2024-06-22T01:52:04.998Z",
        queryTimeMS: 1719021124998,
      },

      panels: range(0, 8).map((i) => ({
        name: "panel-" + i,
        index: i,
        status: ["OK", "ERROR"][i % 2],
        queryTime: "2024-06-17T05:24:00.758Z",
        details: {
          index: 0,
          linkStatus: {
            signal_s: "OK",
            dataPath: "OK",
          },
          fanStatus: "OK",
          brightData: "OK",
          voltage_v: "3.70",
          uptime_h: 723,
          panelID: "50-d3-9a-60-3b-80",
          queryTime: "2024-06-20T07:53:50.695Z",
        },
        healthDesc: {
          description: [HealthDescEnum.NORMAL],
        },
      })),
    },
  };
};

/**
 * 加载带 坏像素的屏
 */
export const v3proMockScreenAllStatusWithBadPixel = () => {
  return JSON.parse(
    fs
      .readFileSync("D:\\coding\\sansi\\v3pro\\v3pro-proxy-server\\__docs\\allstatus.json")
      .toString("utf-8"),
  );
};
